<template>
  <el-card>
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input
          v-model="queryInfo.key"
          placeholder="请输入内容"
          clearable
          @clear="pageDepartment(1, 10)"
        >
          <!-- 搜索框按钮 -->
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="pageDepartment(1, 10)"
          />
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button class="editButton" @click="addDepartmentButton"
          >添加部门</el-button
        >
      </el-col>
    </el-row>

    <div style="margin-top: 15px">
      <el-table
        v-loading="loading"
        :data="pageData"
        fit
        highlight-current-row
        current-row-key
        stripe
        border
        style="width: 100%"
        :row-style="{ height: '80px' }"
      >
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="departmentName" label="部门名" width="180">
        </el-table-column>
        <el-table-column prop="unit.name" label="单位名"></el-table-column>
        <el-table-column prop="gmtCreate" label="创建时间"></el-table-column>
        <el-table-column prop="gmtModified" label="更新时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              class="browserButton"
              icon="el-icon-setting"
              size="mini"
              @click="getDepartmentById(scope.row)"
              >编辑</el-button
            >
            <el-button
              class="deleteButton"
              icon="el-icon-delete"
              size="mini"
              @click="deleteDepartmentById(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div style="margin-top: 15px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog
      title="添加部门"
      :visible.sync="addVisible"
      width="50%"
      :before-close="addHandleClose"
    >
      <div>
        <el-form ref="addVisibleForm" :model="saveData" label-width="80px">
          <el-form-item label="部门名">
            <el-input v-model="saveData.departmentName"></el-input>
          </el-form-item>
          <el-form-item label="单位名">
            <el-select v-model="saveData.unitId" placeholder="请选择所属单位">
              <el-option
                v-for="(item, i) in allUnit"
                :label="item.name"
                :value="item.id"
                :key="i"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="deleteButton" @click="addHandleClose">取 消</el-button>
        <el-button class="browserButton" @click="addDepartment">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="编辑部门"
      :visible.sync="editVisible"
      width="50%"
      :before-close="editHandleClose"
    >
      <div>
        <el-form ref="editVisibleForm" :model="deparentment" label-width="80px">
          <el-form-item label="部门名">
            <el-input v-model="deparentment.departmentName"></el-input>
          </el-form-item>
          <el-form-item label="单位名">
            <el-select
              v-model="deparentment.unitId"
              placeholder="请选择所属单位"
            >
              <el-option
                v-for="(item, i) in allUnit"
                :label="item.name"
                :value="item.id"
                :key="i"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-input v-model="deparentment.gmtCreate" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="更新时间">
            <el-input v-model="deparentment.gmtModified" :disabled="true"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="deleteButton" @click="editHandleClose">取 消</el-button>
        <el-button class="browserButton"  @click="editDepartment">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  pageDepartment,
  getAllDepartment,
  getDepartmentById,
  saveDepartment,
  deleteDepartmentById,
  updateDepartment,
} from "@/api/department";
import { getAllUnit } from "@/api/unit";

export default {
  data() {
    return {
      loading: true,
      addVisible: false,
      saveData: {
        departmentName: "",
        unitId: "",
      },
      allUnit: [],
      queryInfo: { key: null },
      pageData: [],
      total: 0,
      currentPage: 1,
      size: 10,
      deparentment: "",
      editVisible: false,
    };
  },
  created() {
    this.pageDepartment(1, 10);
    this.getAllUnit();
  },
  watch: {},
  methods: {
    pageDepartment(current, limit) {
      if (current < 1) {
        return;
      }
      if (this.queryInfo.key != null) {
        // console.log("aaaa");
        let name = this.queryInfo.key;
        pageDepartment(current, limit, name).then((res) => {
          this.pageData = res.data.list;
          this.total = res.data.total;
          if (res.data.list.length === 0)
            this.pageDepartment(--this.currentPage, this.size);
          this.loading = false;
        });
      } else {
        pageDepartment(current, limit).then((res) => {
          this.pageData = res.data.list;
          this.total = res.data.total;
          if (res.data.list.length === 0)
            this.pageDepartment(--this.currentPage, this.size);
          this.loading = false;
        });
      }
    },

    getDepartmentById(row) {
      getDepartmentById(row.id).then((res) => {
        this.deparentment = res.data.deparentment;
        this.editVisible = true;
      });
    },

    editDepartment() {
      this.dialogVisible = false;
    },
    deleteDepartmentById(row) {
      this.$confirm("此操作将永久删除该单位, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteDepartmentById(row.id).then((res) => {
            this.pageDepartment(this.currentPage, this.size);
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    addDepartmentButton() {
      this.getAllUnit();
      this.addVisible = true;
    },

    addDepartment() {
      saveDepartment(this.saveData)
        .then((res) => {
          this.pageDepartment(this.currentPage, this.size);
          this.addVisible = false;
        })
        .catch((res) => {
          console.log(res);
        });
    },

    getAllUnit() {
      getAllUnit().then((res) => {
        this.allUnit = res.data.list;
      });
    },

    addHandleClose(addVisibleForm) {
      this.addVisible = false;
      this.saveData = this.$options.data().saveData;
    },

    editHandleClose(editVisibleForm) {
      this.editVisible = false;
    },

    handleCurrentChange(newPage) {
      this.currentPage = newPage;
      this.pageDepartment(newPage, this.size);
    },

    handleSizeChange(newSize) {
      this.size = newSize;
      this.pageDepartment(this.currentPage, newSize);
    },
  },
};
</script>

<style>
</style>